package com.hao.tool.web.module.navigation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hao.tool.common.enums.AuthTypeEnum;
import com.hao.tool.web.module.navigation.dto.ModelTypeDTO;
import com.hao.tool.web.module.navigation.entity.ModelTypeInfo;
import com.hao.tool.web.module.navigation.mapper.ModelTypeMapper;
import com.hao.tool.web.module.navigation.service.ModelTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-21 21:45
 */
@Service
public class ModelTypeServiceImpl extends ServiceImpl<ModelTypeMapper, ModelTypeInfo> implements ModelTypeService {
    @Override
    public List<ModelTypeInfo> find(Integer useType, String userName) {
        QueryWrapper<ModelTypeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("SORT");
        queryWrapper.eq(useType != null, "STATUS", useType);

        if (StringUtils.isNotBlank(userName)) {
            queryWrapper
                    .and(wrapper -> wrapper.eq("AUTH_TYPE", AuthTypeEnum.PROTECTED.getCode())
                            .or().eq("AUTH_TYPE", AuthTypeEnum.PUBLIC.getCode())
                            .or(wr -> wr.eq("AUTH_TYPE", AuthTypeEnum.PRIVATE.getCode()).eq("CREATE_USER", userName)));

        } else {
            queryWrapper.and(wrapper -> wrapper.eq("AUTH_TYPE", AuthTypeEnum.PUBLIC.getCode()));
        }
        return list(queryWrapper);
    }

    @Override
    public ModelTypeInfo addModelType(ModelTypeDTO dto, String userName) {
        ModelTypeInfo info = new ModelTypeInfo();

        info.setId(null)
                .setAuthType(dto.getAuthType())
                .setModelName(dto.getModelName())
                .setModelMark(dto.getModelMark())
                .setCreateUser(userName)
                .setContent(dto.getContent())
                .setCreateTime(LocalDateTime.now())
                .setLogoUrl(dto.getLogoUrl())
                .setModelClass(dto.getModelClass())
                .setSort(dto.getSort())
                .setBadgeClass(dto.getBadgeClass())
                .setStatus(dto.getStatus())
                .setTagGroup(dto.getTagGroup())
                .setCode(dto.getCode())
                .setModelType(dto.getModelType());
        this.save(info);
        return info;
    }

    @Override
    public ModelTypeInfo findByModelMark(String mark) {
        QueryWrapper<ModelTypeInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("MODEL_MARK", mark);
        return this.getOne(wrapper);
    }
}
